/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2013-11-26         luwei
 */

package com.tianci.appstore.ui.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.utils.LogUtils;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.SkinManager;
import com.tianci.appstore.ui.data.SkyAppBaseData;
import com.tianci.appstore.utils.SkyConstants;
import com.tianci.appstore.utils.SkyNetImageCache.ImageLoadListener;

public abstract class SkyBaseItemView extends FrameLayout implements OnKeyListener, OnClickListener,
        OnGestureListener, OnTouchListener, OnFocusChangeListener, ImageLoadListener {
    protected static final String ICON_EXT = "icon";
    protected static final String EXTRA = "extra";
    protected int mIndex = 0;
    protected boolean[] mTurnPage = null;
    protected Context mContext = null;
    private GestureDetector mGestureDetector;
    //    private OnAppItemClickListener mClickListener = null;
    private OnAppKeyListener mKeyListener = null;

    protected SkyAppBaseData data;
    protected TextView titleText;
    protected boolean selected = false;

    protected boolean canHandleKey = true;
//    protected FrameLayout mainLayout;

    protected static int iconWidth = SkyConstants.iconWidth;
    protected boolean isChildrenMode = false;

//    public interface OnAppItemClickListener
//    {
//        public void onClick(int index);
//    }

    public interface OnAppKeyListener {
        public boolean onKeyDown(int keyCode, int index, boolean[] turnPage);
    }

    public SkyBaseItemView(Context context) {
        super(context);
        this.mContext = context;
        setFocusable(true);
        setFocusableInTouchMode(true);
        setOnKeyListener(this);
        setOnTouchListener(this);
        setOnClickListener(this);
        mGestureDetector = new GestureDetector(this);
        setOnFocusChangeListener(this);

        setChildrenMode(isChildrenMode);
        createView();
    }

    protected abstract void createView();

    public void setTurnPage(boolean[] turnpage) {
        this.mTurnPage = turnpage;
    }

    public SkyAppBaseData getData() {
        return data;
    }

//    public void setClickListener(OnAppItemClickListener listener)
//    {
//        this.mClickListener = listener;
//    }

    public void setKeyListener(OnAppKeyListener listener) {
        this.mKeyListener = listener;
    }

    public void setIndex(int index) {
        this.mIndex = index;
    }

    public static boolean mKey = true;

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (!mKey) {
            return true;
        }
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_ENTER:
                case KeyEvent.KEYCODE_DPAD_CENTER:
                case KeyEvent.KEYCODE_DPAD_LEFT:
                case KeyEvent.KEYCODE_DPAD_RIGHT:
                case KeyEvent.KEYCODE_DPAD_UP:
                case KeyEvent.KEYCODE_DPAD_DOWN:
                    if (mKeyListener != null)
                        return mKeyListener.onKeyDown(keyCode, mIndex, mTurnPage);
                    break;
                default:
                    break;
            }
        }
        return false;
    }

    @Override
    public void onClick(View v) {
    }

    @Override
    public boolean onDown(MotionEvent e) {
//        if (mClickListener != null)
//        {
//            mClickListener.onClick(mIndex);
//            return true;
//        }
        return false;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return mGestureDetector.onTouchEvent(event);
    }

    public void setSelected(boolean selected) {
        // TODO Auto-generated method stub
        if (selected)
            this.requestFocus();
        else
            this.clearFocus();
    }

    public void setChildrenMode(boolean childrenMode) {
        this.isChildrenMode = childrenMode;
        if (childrenMode) {
            SkinManager.setBackground(this, R.drawable.block_selector_child);
            this.setPadding(SkyConstants.shadowChildWidth, SkyConstants.shadowChildWidth,
                    SkyConstants.shadowChildWidth, SkyConstants.shadowChildWidth);
        } else {
            SkinManager.setBackground(this, R.drawable.block_selector);
            this.setPadding(SkyConstants.shadowWidth, SkyConstants.shadowWidth,
                    SkyConstants.shadowWidth, SkyConstants.shadowWidth);
        }
    }

    @Override
    public void onFocusChange(View arg0, boolean hasFocus) {
        // TODO Auto-generated method stub
//        if(hasFocus)
//        {
//            SkinManager.setBackground(this, R.drawable.block_focused);
//        }
//        else{
//            SkinManager.setBackground(this, R.drawable.block_bg);
//        }
        this.setPadding(SkyConstants.shadowWidth, SkyConstants.shadowWidth, SkyConstants.shadowWidth, SkyConstants.shadowWidth - CoocaaApplication.Div(2));
        if (titleText != null) {
            titleText.setSelected(hasFocus);
        }
        this.selected = hasFocus;
    }

    public abstract void update(SkyAppBaseData data);

    protected Handler mainHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                String url = msg.getData().getString("url");
                Bitmap bitmap = (Bitmap) msg.obj;
                refreshImage(url, bitmap);
            }
            super.handleMessage(msg);
        }
    };

    protected abstract void refreshImage(String url, Bitmap result);

    @Override
    public void loadFinished(String url, Bitmap result) {
        // TODO Auto-generated method stub
        LogUtils.d("big", "loadFinished---url:" + url);
        if (result == null) {
            LogUtils.i("lqw", "Load image == null");
            return;
        }
        Message msg = mainHandler.obtainMessage(0);
        msg.obj = result;
        Bundle bundle = new Bundle();
        bundle.putString("url", url);
        msg.setData(bundle);
        mainHandler.sendMessage(msg);
    }

    public int getIndex() {
        return mIndex;
    }

    public abstract void destory();
}
